$(function () {

    var plan_data = $('#plan_data').val();

    plan_data = $.parseJSON(plan_data);

    var chart_pie2 = echarts.init(document.getElementById('pieChart2'));

    for (var k = 0; k < plan_data.length; k++) {
        plan_data[k] = eval('(' + plan_data[k] + ')');
    }
    var option_pie2 = {
        backgroundColor: 'rgba(255,255,255, 0)',
        title: {
            text: '年1000万计划完成情况',
            subtext: '好好干，天天有米饭',
            x: 'center'
        },
        tooltip: {
            trigger: 'item',
            formatter: "{a} <br/>{b} : {c} ({d}%)"
        },
        color: ['#8fc31f', '#00ccff'],
        legend: {
            orient: 'vertical',
            x: 'left',
            data: ['已完成金额（万）', '未完成金额（万）'],
        },
        toolbox: {
            show: true,
            feature: {
                mark: {show: true},
                dataView: {show: true, readOnly: false},
                magicType: {
                    show: true,
                    type: ['pie', 'funnel'],
                    option: {
                        funnel: {
                            x: '25%',
                            width: '50%',
                            funnelAlign: 'left',
                            max: 1548
                        }
                    }
                },
                restore: {show: true},
                saveAsImage: {show: true}
            }
        },
        calculable: true,
        series: [
            {
                name: ['年1000万计划完成情况'],
                type: 'pie',
                radius: '55%',
                center: ['50%', '60%'],
                data: plan_data,
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    },
                    normal: {
                        label: {
                            show: true,
                            //position:'inside',
                            formatter: '{b} : {c} ({d}%)'
                        }
                    },
                    labelLine: {show: false}
                }
            }
        ]
    };

    chart_pie2.setOption(option_pie2);

    var _csrf = $('input[name="_csrf-backend"]').val();
    //var plan_show;

    var plan_show = window.setInterval(function () {
        //option_pie2.series[0].data = {};
        $('#plan_data').val('');
        $.ajax({
            url: 'get-data',
            type: 'POST',
            dataType: "json",
            data: {'_csrf-backend': _csrf},
            error: function (xhr) {

            },
            success: function (data) {

                var chart_pie2 = echarts.init(document.getElementById('pieChart2'));

                for (var k = 0; k < data.length; k++) {
                    data[k] = eval('(' + data[k] + ')');
                }

                option_pie2.series[0].data = data;

                chart_pie2.setOption(option_pie2);
            }
        });
    }, 8000);


    //plan_complete();

    var data_url = [
        [
            '', ''
        ]
    ];

    function everyClick2(param, i, txt, url) {    //程序这边的url需要传入
        if (param.seriesIndex == 0 && param.dataIndex == i) {
            window.open(url, '_parent', 'height=100,width=400,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no');
        }
    }

    //增加监听事件
    function eConsole2(param) {
        //var mes = '【' + param.type + '】';
        if (typeof param.seriesIndex != 'undefined') {
            // mes += '  seriesIndex : ' + param.seriesIndex;
            // mes += '  dataIndex : ' + param.dataIndex;
            if (param.type == 'click') {
                var peiLenght = option_pie2.legend.data.length;
                // alert(peiLenght);// 获取总共给分隔的扇形数
                for (var i = 0; i < peiLenght; i++) {
                    everyClick2(param, i, option_pie2.legend.data[i], data_url[1][i])
                }
            } else {

                document.getElementById('hover-console').innerHTML = 'Event Console : ' + param.dataIndex;
                //alert();
            }

        }
    }

    chart_pie2.on("click", eConsole2);
    chart_pie2.on("hover", eConsole2);

});